package com.upc.cajabancos.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import jxl.Workbook;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.write.Label;
import jxl.write.NumberFormat;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.upc.cajabancos.model.Banco;
import com.upc.cajabancos.model.PagoBanco;
import com.upc.cajabancos.service.BancoService;
import com.upc.cajabancos.service.PagoBancoService;

public class ReporteAction extends DispatchAction {
	
	@Override
	public ActionForward execute(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {

		
		if(!arg2.isRequestedSessionIdValid()){
			return arg0.findForward("login");
		}
		
		return super.execute(arg0, arg1, arg2, arg3);
	}

	/**
	 * Inicia la pagina del reporte
	 * @author Cesar Jesus
	 * @since 29/01/2011
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward pagoBancos(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		System.out.println("Llego a pagoBancos");
		
		HttpSession httpSession = request.getSession();
		
		
		
		BancoService bancoService = new BancoService();
		
		List<Banco> listaBancos = bancoService.listar();
		
		httpSession.setAttribute("listaBancos", listaBancos);
		
		return mapping.findForward("pagoBancos");
	}
	
	/**
	 * Genera reporte en html
	 * @author Cesar Jesus
	 * @since 29/01/2011
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward generarListado(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		System.out.println("Llego a pagoBancos");
		
		HttpSession httpSession = request.getSession();
		
		request.setAttribute("reporte", request.getParameter("chkReporte"));
		String codBanco = request.getParameter("cboBanco");
		String desde = request.getParameter("txtDesde");
		String hasta = request.getParameter("txtHasta");
		
		DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
		Date fechaDesde = null;
		Date fechaHasta = null;
		if(!desde.equals("")){
			fechaDesde = (Date)formatter.parse(desde);
		}
		if(!hasta.equals("")){
			fechaHasta = (Date)formatter.parse(hasta);
		}
		
		System.out.println("fechaDesde " + fechaDesde);
		System.out.println("fechaHasta " + fechaHasta);
		
		PagoBancoService pagoBancoService = new PagoBancoService();
		
		List<PagoBanco> listaPagoBancos = pagoBancoService.buscar(codBanco, fechaDesde, fechaHasta);
		
		httpSession.setAttribute("listaPagoBancos", listaPagoBancos);
		httpSession.setAttribute("idBanco", codBanco);
		httpSession.setAttribute("desde", desde);
		httpSession.setAttribute("hasta", hasta);
		
		return mapping.findForward("pagoBancos");
	}
	
	/**
	 * Genera reporte en excel
	 * @author Cesar Jesus
	 * @since 29/01/2011
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward generarExcel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		System.out.println("Llego a pagoBancos");
		
		HttpSession httpSession = request.getSession();
		
		request.setAttribute("reporte", request.getParameter("chkReporte"));
		String codBanco = request.getParameter("cboBanco");
		String desde = request.getParameter("txtDesde");
		String hasta = request.getParameter("txtHasta");
		
		DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
		Date fechaDesde = null;
		Date fechaHasta = null;
		if(!desde.equals("")){
			fechaDesde = (Date)formatter.parse(desde);
		}
		if(!hasta.equals("")){
			fechaHasta = (Date)formatter.parse(hasta);
		}
		
		PagoBancoService pagoBancoService = new PagoBancoService();		
		List<PagoBanco> listaPagoBancos = pagoBancoService.buscar(codBanco, fechaDesde, fechaHasta);
		
		Banco banco = buscarBanco(httpSession, codBanco);
		
		
		
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-Disposition", "attachment; filename=reporte.xls");
		WritableWorkbook workbook = Workbook.createWorkbook(response.getOutputStream());

		WritableSheet sheet = workbook.createSheet("Reporte Pago en Bancos", 0);
		sheet.setColumnView(0,5);
		sheet.setColumnView(1,22);
		sheet.setColumnView(2,20);
		sheet.setColumnView(3,15);
		sheet.setColumnView(4,15);
		sheet.setColumnView(5,15);
		
		Label label = null;

		WritableFont titulo = new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD, false);
		WritableCellFormat tituloformat = new WritableCellFormat(titulo);

		WritableFont encabezado = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD, false);
		WritableCellFormat encabezadoformat = new WritableCellFormat(encabezado);
	

		WritableFont normal = new WritableFont(WritableFont.ARIAL, 9, WritableFont.NO_BOLD, false);
		WritableCellFormat normalformat1 = new WritableCellFormat(normal);
	
		WritableCellFormat encabezadoformatGrey = new WritableCellFormat(encabezado);
		encabezadoformatGrey.setBorder(Border.ALL, BorderLineStyle.THIN);
		encabezadoformatGrey.setBackground(Colour.GRAY_25);
	
		int col = 1;
		int fil = 1;
		label = new Label(col, fil, "Reporte Pago en Bancos", tituloformat);
		sheet.addCell(label); 

		label = new Label(col, fil+2, "Banco: ", encabezadoformat);
		sheet.addCell(label); 
		
		label = new Label(col, fil+3, "Fecha Desde: ", encabezadoformat);
		sheet.addCell(label);
		
		label = new Label(col, fil+4, "Fecha Hasta: ", encabezadoformat);
		sheet.addCell(label);
		
		label = new Label(col+1, fil+2, (banco==null?"Todos":banco.getNombre()),normalformat1);
		sheet.addCell(label); 
		
		label = new Label(col+1, fil+3, desde, normalformat1);
		sheet.addCell(label);
		
		label = new Label(col+1, fil+4, hasta, normalformat1);
		sheet.addCell(label);
				
		NumberFormat decimal_2 = new NumberFormat("############0.00"); 
		WritableCellFormat decimal_2Format = new WritableCellFormat(decimal_2); 

		jxl.write.Number number = null; 
	 
		fil = fil + 5; 
		
		label = new Label(col, fil,"Banco",encabezadoformatGrey);
		sheet.addCell(label);
		label = new Label(col+1, fil,"Agencia",encabezadoformatGrey);
		sheet.addCell(label); 
		label = new Label(col+2, fil,"Fecha",encabezadoformatGrey);
		sheet.addCell(label);
		label = new Label(col+3, fil,"Monto",encabezadoformatGrey);
		sheet.addCell(label);
				
		fil = fil + 1;
		
		PagoBanco pagoBanco = null;
		for (int i = 0; i < listaPagoBancos.size(); i++) {
			pagoBanco = listaPagoBancos.get(i);          
				
			label = new Label(col, fil+i,pagoBanco.getBanco().getNombre(),normalformat1);
			sheet.addCell(label); 
			label = new Label(col+1, fil+i,pagoBanco.getAgencia(),normalformat1);
			sheet.addCell(label);
			label = new Label(col+2, fil+i,pagoBanco.getFechaFormateada(),normalformat1);
			sheet.addCell(label);	
			number = new jxl.write.Number(col+3, fil+i, pagoBanco.getMontoPagado(),decimal_2Format);
			sheet.addCell(number);
		}

		workbook.write(); 
		workbook.close(); 
		
		return null;
	}
	
	/**
	 * Sale al menu principal
	 * @author Cesar Jesus
	 * @since 29/01/2011
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward salir(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		System.out.println("Llego a salir");
		
		return mapping.findForward("principal");
	}
	
	private Banco buscarBanco(HttpSession httpSession, String codigoBanco){
		
		List<Banco> listaBancos = (List<Banco>)httpSession.getAttribute("listaBancos");
		
		for(Banco banco : listaBancos){
			
			if(banco.getCodigo().equals(codigoBanco)){
				return banco;
			}
			
		}
		return null;
	}

}
